{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "抗击肺炎\n",
      "新闻\n",
      "hao123\n",
      "地图\n",
      "视频\n",
      "贴吧\n",
      "学术\n",
      "登录\n",
      "设置\n",
      "更多产品\n",
      "设为首页关于百度About Baidu百度推广使用百度前必读意见反馈帮助中心\n",
      "下载百度APP\n",
      "有事搜一搜  没事看一看\n",
      "百度一下，你就知道\n",
      "[{'domain': 'www.baidu.com', 'expires': '周四, 26 3月 2020 14:05:44 GMT', 'expiry': 1585231544, 'httponly': False, 'name': 'WWW_ST', 'path': '/', 'secure': False, 'value': '1585231534084'}, {'domain': 'www.baidu.com', 'expires': '周日, 05 4月 2020 14:05:33 GMT', 'expiry': 1586095533, 'httponly': False, 'name': 'BD_UPN', 'path': '/', 'secure': False, 'value': '14314454'}, {'domain': '.baidu.com', 'expires': '周一, 18 3月 2052 14:05:33 GMT', 'expiry': 2594383533, 'httponly': False, 'name': 'BIDUPSID', 'path': '/', 'secure': False, 'value': '92EAF74F1C162385FF37CC0BF296B427'}, {'domain': '.baidu.com', 'httponly': False, 'name': 'H_PS_PSSID', 'path': '/', 'secure': False, 'value': '1421_31124_21108_30840_31187_30824_26350'}, {'domain': 'www.baidu.com', 'httponly': False, 'name': 'BD_HOME', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'www.baidu.com', 'expires': '周四, 26 3月 2020 14:05:34 GMT', 'expiry': 1585231534, 'httponly': False, 'name': 'BD_LAST_QID', 'path': '/', 'secure': False, 'value': '15939838037460882705'}, {'domain': '.baidu.com', 'expires': '周五, 26 3月 2021 14:05:36 GMT', 'expiry': 1616767536, 'httponly': False, 'name': 'BAIDUID', 'path': '/', 'secure': False, 'value': '1F39A091C5549D21D2953D84EFBB2B1D:FG=1'}, {'domain': '.baidu.com', 'expires': '周二, 13 4月 2088 17:19:40 GMT', 'expiry': 3732715180, 'httponly': False, 'name': 'PSTM', 'path': '/', 'secure': False, 'value': '1585231536'}]\n",
      "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E9%95%BF%E5%9F%8E&rsv_t=e679ZT9LgxZes%2B9UfDUQWZNfpF4KD5XYho6kC06YRb0P5nv80bDyZndUPyw&rsv_enter=0&rsv_dl=ib&rsv_sug3=2&inputT=51&rsv_sug4=51\n"
     ]
    }
   ],
   "source": [
    "# 导入 webdriver\n",
    "from selenium import webdriver\n",
    "# 要想调用键盘按键操作需要引入keys包\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "# 调用环境变量指定的PhantomJS浏览器创建浏览器对象\n",
    "driver = webdriver.PhantomJS()\n",
    "# 如果没有在环境变量指定PhantomJS位置# driver = webdriver.PhantomJS(executable_path=\"./phantomjs\"))\n",
    "# get方法会一直等到页面被完全加载，然后才会继续程序，通常测试会在这里选择 time.sleep(2)\n",
    "driver.get(\"http://www.baidu.com/\")\n",
    "# 获取页面名为 wrapper的id标签的文本内容\n",
    "data = driver.find_element_by_id(\"wrapper\").text\n",
    "# 打印数据内容\n",
    "print(data)\n",
    "# 打印页面标题 \"百度一下，你就知道\n",
    "print(driver.title)\n",
    "# 生成当前页面快照并保存\n",
    "driver.save_screenshot(\"baidu.png\")\n",
    "# id=\"kw\"是百度搜索输入框，输入字符串\"长城\"\n",
    "driver.find_element_by_id(\"kw\").send_keys(\"长城\")\n",
    "# id=\"su\"是百度搜索按钮，click() 是模拟点击\n",
    "driver.find_element_by_id(\"su\").click()\n",
    "# 获取新的页面快照\n",
    "driver.save_screenshot(\"长城.png\")\n",
    "# 打印网页渲染后的源代码\n",
    "# print(driver.page_source)\n",
    "# 获取当前页面Cookie\n",
    "print(driver.get_cookies())\n",
    "# ctrl+a 全选输入框内容\n",
    "driver.find_element_by_id(\"kw\").send_keys(Keys.CONTROL,'a')\n",
    "# ctrl+x 剪切输入框内容\n",
    "driver.find_element_by_id(\"kw\").send_keys(Keys.CONTROL,'x')\n",
    "# 输入框重新输入内容\n",
    "driver.find_element_by_id(\"kw\").send_keys(\"atguigu\")\n",
    "# 模拟Enter回车键\n",
    "# driver.find_element_by_id(\"su\").send_keys(Keys.RETURN)\n",
    "# 清除输入框内容\n",
    "driver.find_element_by_id(\"kw\").clear()\n",
    "# 生成新的页面快照\n",
    "driver.save_screenshot(\"atguigu.png\")\n",
    "# 获取当前url\n",
    "print(driver.current_url)\n",
    "# 关闭当前页面，如果只有一个页面，会关闭浏览器# driver.close()\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import unittest\n",
    "from selenium import webdriver\n",
    "from bs4 import BeautifulSoup as bs\n",
    "import time\n",
    "\n",
    "\n",
    "class douyu(unittest.TestCase):\n",
    "    #初始化方法,必须是setUp（）\n",
    "    def __init__(self):\n",
    "        self.driver = webdriver.Chrome()\n",
    "        self.num = 0\n",
    "        self.count = 0\n",
    "        self.l1 = set()\n",
    "\n",
    "    #测试方法必须有test字样开头\n",
    "#     def testDouyu(self):\n",
    "        \n",
    "\n",
    "    #测试结束执行的方法\n",
    "    def tearDown(self):\n",
    "        # print(\"当前直播人数：\"+str(self .num))\n",
    "        # print(\"当前观看人数：\"+str(self .count))\n",
    "\n",
    "        self.driver.quit()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "130"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try:\n",
    "    d = douyu()\n",
    "    d.driver.get(\"https://www.douyu.com/directory/all\")\n",
    "    for i in range(2):\n",
    "        soup = bs(self.driver.page_source, \"html.parser\")\n",
    "        # 房间名，返回列表\n",
    "        names = soup.find_all(\"h3\", {\"class\": \"DyListCover-intro\"})\n",
    "        del names[:10]\n",
    "        #观众人数，返回列表\n",
    "        numbers = soup.find_all(\"span\", {\"class\": \"DyListCover-hot\"})\n",
    "        # print(names[1].get_text().strip()+\"  \"+numbers[1].get_text().strip())\n",
    "        #zip(names,numbers)将name和number这两个列表合并为一个元组：[（1,2），（3,4）.....]\n",
    "        for name, number in zip(names, numbers):\n",
    "            num = number.get_text().strip()\n",
    "            name = name.get_text().strip()\n",
    "    #                 print(\"观众人数：\" + num + \"\\t房间名:\" + name)\n",
    "            self.l1.add((name, num))\n",
    "\n",
    "        time.sleep(1)\n",
    "        d.driver.find_element_by_class_name(\"dy-Pagination-item-custom\").click()\n",
    "        print(d.l1)\n",
    "except Exception:\n",
    "    print(Exception.with_traceback)\n",
    "finally:\n",
    "    d.driver.quit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver.common.keys import Keys\n",
    "\n",
    "d = douyu()\n",
    "d.driver.get(\"https://www.douyu.com/directory/all\")\n",
    "def scrollToEnd1(driver):\n",
    "    h1=0\n",
    "    h2=1\n",
    "    while h1!=h2:\n",
    "        h1 = d.driver.execute_script(\"return document.body.scrollHeight;\")\n",
    "        d.driver.execute_script(\"window.scrollBy(0,1200)\")\n",
    "        time.sleep(0.1)\n",
    "        h2 = d.driver.execute_script(\"return document.body.scrollHeight;\")\n",
    "        \n",
    "def scrollToEnd2(driver):\n",
    "    body=driver.find_element_by_css_selector(\"body\")\n",
    "    time.sleep(0.1)\n",
    "    body.click()\n",
    "    body.send_keys(Keys.END)\n",
    "    body.send_keys(Keys.PAGE_DOWN)\n",
    "\n",
    "scrollToEnd2(d.driver)\n",
    "d.driver.find_element_by_class_name(\"dy-Pagination-next\").click()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
